ggplot: grammar of graphics plot
sample_dat <- rnorm(100, 0, 1)
sample2_dat <- rnorm(100, 10, 5)
hist(sample_dat)
plot(sample_dat, sample2_dat)
library(gapminder)
## Warning: package 'gapminder' was built under R version 3.5.3
library(tidyverse)
## Warning: package 'tidyverse' was built under R version 3.5.3
## -- Attaching packages ------------------------------------------------ tidyverse 1.2.1 --
## √ ggplot2 3.1.0 √ purrr 0.3.1
## √ tibble 2.0.1 √ dplyr 0.8.0.1
## √ tidyr 0.8.3 √ stringr 1.4.0
## √ readr 1.3.1 √ forcats 0.4.0
## Warning: package 'ggplot2' was built under R version 3.5.3
## Warning: package 'tibble' was built under R version 3.5.3
## Warning: package 'tidyr' was built under R version 3.5.3
## Warning: package 'readr' was built under R version 3.5.3
## Warning: package 'purrr' was built under R version 3.5.3
## Warning: package 'dplyr' was built under R version 3.5.3
## Warning: package 'stringr' was built under R version 3.5.3
## Warning: package 'forcats' was built under R version 3.5.3
## -- Conflicts --------------------------------------------------- tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
gapminder %>%
filter(continent %in% c("Africa", "Asia")) %>%
ggplot(aes(x=gdpPercap, y=lifeExp, color=continent)) +
geom_point(alpha=0.5) +
scale_x_log10(labels = scales::comma) +
scale_y_continuous(labels = scales::percent) +
labs(title = "1인당 국민소득과 기대수명",
subtitle = "UN 5년 간 시계열 데이터",
x = "1인당 국민소득",
y = "기대수명",
color = "대륙",
caption = "자료출처: http:// www.tidtverse.com") +
facet_wrap(~continent) +
theme(legend.position = "bottom")
### Basic component - aes: x-axis, y-axis, color(~category) - geom: type of plot - scale: x-range, y-range cf. sclae_x_(transformation)
library(plotly)
## Warning: package 'plotly' was built under R version 3.5.3
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
gapminder_g <- gapminder %>%
filter(continent %in% c("Africa", "Asia")) %>%
ggplot(aes(x=gdpPercap, y=lifeExp, color=continent,
text=paste0("1인당 GDP:", scales::comma(gdpPercap), "\n",
"기대수명: ", lifeExp))) +
geom_point(alpha=0.5) +
scale_x_sqrt(labels = scales::comma) +
labs(title="1인당 국민소득과 기대수명",
subtitle="UN 5년 시계열 데이터",
color="대륙",
x="1인당 국민소득",
y="기대수명",
caption="자료출처: http://www.tidyverse.com") +
theme(legend.position = "none")
gapminder_g
ggplotly(gapminder_g, tooltip="text")
boxplot_g <- gapminder %>%
filter(continent %in% c("Africa", "Asia")) %>%
ggplot(aes(x=continent, y=lifeExp, color=continent)) +
geom_boxplot(alpha=0.5)
library(gridExtra)
## Warning: package 'gridExtra' was built under R version 3.5.3
##
## Attaching package: 'gridExtra'
## The following object is masked from 'package:dplyr':
##
## combine
grid.arrange(gapminder_g, boxplot_g, nrow=1)